package cn.leedsoft.and.crimeintent.controller.run_tracker;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

/**
 * Created by leeds on 2016/9/5 16:47.
 */
public class LocationReceiver extends BroadcastReceiver {
    private static final String TAG = "LocationReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {

        Location loc = intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
        if(loc != null) {
            //parse location data
            onLocationReceived(context, loc);
            return;
        }

        if(intent.hasExtra(LocationManager.KEY_PROVIDER_ENABLED)) {//关闭-开启的时候，会有这个extra？
            boolean enabled = intent.getBooleanExtra(LocationManager.KEY_PROVIDER_ENABLED, false);
            onProviderEnabled(enabled);
        }
    }

    protected void onProviderEnabled(boolean enabled) {
        Log.i(TAG, "location provider enabled : " + enabled);
    }

    protected void onLocationReceived(Context context, Location loc) {
        Log.i(TAG, "got location from provider: " + loc.getProvider() + ". latitude: " + loc.getLatitude() + ". longitude: " + loc.getLongitude() + ". altitude: " + loc.getAltitude());
    }
}
